<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="数据中心" prop="dataCenter">
        <el-select v-model="queryParams.dataCenter" placeholder="请选择数据中心" clearable>
          <el-option
            v-for="dict in dict.type.data_center"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="数据备份状态" prop="dataBackupStatus">
        <el-select v-model="queryParams.dataBackupStatus" placeholder="请选择数据中心" clearable>
          <el-option
            v-for="dict in dict.type.data_backup_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备份时间" prop="backupTime">
        <el-date-picker
          v-model="backupTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          @change="changeBackupTime">
        </el-date-picker>
      </el-form-item>
      <!-- <el-form-item label="创建时间" prop="createTime">
        <el-date-picker clearable
          v-model="queryParams.createTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleSync"
          v-hasPermi="['system:inspection:add']"
        >数据同步</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:inspection:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:inspection:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:inspection:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:inspection:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="inspectionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <el-table-column label="数据中心" align="center" prop="dataCenter">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.data_center" :value="scope.row.dataCenter"/>
        </template>
      </el-table-column>
      <el-table-column label="业务部门" align="center" prop="businessDepartment" />
      <el-table-column label="备份时间" align="center" prop="backupTime">
        <template slot-scope="scope">
          <span>{{ $formatDate(scope.row.backupTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="数据备份状态" align="center" prop="dataBackupStatus">
        <template slot-scope="scope">
          <dict-tag :class="classColor[scope.row.dataBackupStatus]" :options="dict.type.data_backup_status" :value="scope.row.dataBackupStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="备份数据量(GB)" align="center" prop="backupDataVolume" />
      <el-table-column label="备份版本号" align="center" prop="backupVersionNumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon=""
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:inspection:edit']"
          >查看详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon=""
            @click="handleRecover(scope.row)"
            v-hasPermi="['system:inspection:edit']"
          >数据恢复</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:inspection:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改日志巡检对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="42%" append-to-body>
        <div style="height: 500px;overflow: auto;">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="数据中心" prop="dataCenter">
          <el-select v-model="form.dataCenter" placeholder="请选择数据中心" :disabled="true">
            <el-option
              v-for="dict in dict.type.data_center"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备份时间" prop="backupTime">
          <el-input v-model="form.backupTime" :disabled="true" placeholder="请输入备份时间" />
        </el-form-item>
        <el-form-item label="数据备份状态" prop="dataBackupStatus">
          <el-select v-model="form.dataBackupStatus" :disabled="true" placeholder="请选择数据备份状态">
            <el-option
              v-for="dict in dict.type.data_backup_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备份数据量" prop="backupDataVolume">
          <el-input v-model="form.backupDataVolume" :disabled="true" placeholder="请输入备份数据量" />
        </el-form-item>
        <el-form-item label="备份版本号" prop="backupVersionNumber">
          <el-input v-model="form.backupVersionNumber" :disabled="true" placeholder="请输入备份版本号" />
        </el-form-item>
        <!-- <el-form-item label="是否删除" prop="delFlag">
          <el-input v-model="form.delFlag" :disabled="true" placeholder="请输入是否删除" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" :disabled="true" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        
      </el-form>
        </div>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 数据恢复 -->
    <el-dialog :title="'数据恢复'" :visible.sync="openRecover" width="42%" append-to-body>
      <div style="height: 500px;overflow: auto;">
        <el-table v-loading="loading" :data="inspectionListRecover" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="id" align="center" prop="id" /> -->
          <el-table-column label="数据中心" align="center" prop="dataCenter">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.data_center" :value="scope.row.dataCenter"/>
            </template>
          </el-table-column>
          <el-table-column label="业务部门" align="center" prop="businessDepartment" />
          <el-table-column label="备份时间" align="center" prop="backupTime" />
          <el-table-column label="数据备份状态" align="center" prop="dataBackupStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.data_backup_status" :value="scope.row.dataBackupStatus"/>
            </template>
          </el-table-column>
          <el-table-column label="备份数据量" align="center" prop="backupDataVolume" />
          <el-table-column label="备份版本号" align="center" prop="backupVersionNumber" />
          
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <!-- <el-button type="primary" @click="submitForm">确 定</el-button> -->
        <el-button @click="openRecover = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInspection, getInspection, delInspection, addInspection, updateInspection, selectLogInspectionData } from "@/api/system/inspection";
// import { formatDate } from "@/utils/index.js"
export default {
  name: "Inspection",
  dicts: ['data_center', 'data_backup_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日志巡检表格数据
      inspectionList: [],
      inspectionListRecover: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openRecover: false,
      // 查询参数
      backupTime: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dataCenter: null,
        businessDepartment: null,
        timeFrom: null,
        timeTo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeBackupTime(date) {
      console.log(date,'date,value');
      if(date) {
        this.queryParams.timeFrom = date[0]
        this.queryParams.timeTo = date[1]
      }else {
        this.queryParams.timeFrom = ''
        this.queryParams.timeTo = ''
      }
    },
    handleSync() {
      this.$modal.msg("数据同步中...");
    },
    /** 查询日志巡检列表 */
    getList() {
      this.loading = true;
      listInspection(this.queryParams).then(response => {
        this.inspectionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dataCenter: null,
        businessDepartment: null,
        backupTime: null,
        timeFrom: null,
        timeTo: null,
        dataBackupStatus: null,
        backupDataVolume: null,
        backupVersionNumber: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        extend1: null,
        extend2: null,
        extend3: null,
        extend4: null,
        extend5: null,
        extend6: null,
        extend7: null,
        extend8: null,
        extend9: null,
        extend10: null
      };
      
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.backupTime = []
      this.queryParams.timeFrom = ''
      this.queryParams.timeTo = ''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加日志巡检";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInspection(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "日志巡检详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInspection(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspection(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除日志巡检编号为"' + ids + '"的数据项？').then(function() {
        return delInspection(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/inspection/export', {
        ...this.queryParams
      }, `inspection_${new Date().getTime()}.xlsx`)
    },
    handleRecover(row) {
      // 传extend1,id,dataCenter
      let params = {
        pageNum: 1,
        pageSize: 10,
        id: row.id,
        extend1: row.extend1,
        dataCenter: row.dataCenter,
        backupTime: row.backupTime
      }
      selectLogInspectionData(params).then(response => {
        if(response.code == 200) {
          this.inspectionListRecover = response.rows;
          this.openRecover = true;
        }
      });
    }
  }
};
</script>
